(async function(){
    var vote;
    setLoading(1);
    var myGroup = await getMyGroup();
    var rtn = await loadManbers();
    setLoading(0);
    init();
    async function init(){
        if (rtn){
            vote = getNode("detail.js").tag;
            //投票完成后返回的是ID，需要重新加载一下。
            if (typeof vote === 'number'){
                vote = await getVote(vote);
            }
            //判断是否有投票权
            var isin=false;
            if (vote.groupId.toNumber()!=0){
                var group = await getGroup(vote.groupId.toNumber());
                setNodeText("group",group.name);
                for(var i=0;i<myGroup.length;i++){
                    if (myGroup[i].id.toNumber() == vote.groupId.toNumber()){
                        isin=true;
                        break;
                    }
                }
            }else isin=true;
            //发起人
            setNodeText("member",getMemberByAddress(vote.promoter).name);
            //投票标题
            setNodeText("name",vote.name);
            //开始
            setNodeText("start",dateFormat(vote.start));
            //截止
            setNodeText("end",dateFormat(vote.end));
            //开始时间
            setNodeText("body",vote.body.replaceAll("\r\n","<br>"));
            //显示选项
            var options = getNode("options");
            var line = options.children[0];
            clearAll(options);
            for(var i=0;i<vote.options.length;i++){
                var newLine = line.cloneNode(true);
                newLine.children[0].value = i;
                //有权投票的才能看到选择按钮
                display(newLine.children[0],isin);
                //显示选项
                var div = newNode("div");
                div.style.color="white";
                div.style.fontSize="22px";
                div.innerText = vote.options[i];
                newLine.children[1].appendChild(div);
                //显示比例和投票人及理由
                var div1 = newNode("div");
                var jj=0;
                for(var j=0;j<vote.results.length;j++) if (vote.results[j]==i) jj++;
                if (vote.results.length>0) div1.innerText = Math.round(jj/vote.results.length*100,2)+"%";
                newLine.children[1].appendChild(div1);
                options.appendChild(newLine);
            }
            var isEnd = vote.end.toNumber()<time();
            var voted = false;
            //显示已经投票者的选择和理由
            for(var i=0;i<vote.results.length;i++){
                //找到刚才留出的空间
                var div = options.children[vote.results[i]].children[1].children[1];
                var sub = newNode("div");
                var m = getMemberByAddress(vote.voters[i]);
                sub.innerText = m.name+":"+vote.reasons[i];
                div.appendChild(sub);
                //已经投过票了
                if (vote.voters[i] == publicParam.address) voted=true;
            }
            //显示投票按钮
            if (isin && !voted && !isEnd){
                display("isIn",true);
                //投票
                getNode("vote").onclick=async function(){
                    var option = getList("options");
                    var reason = getNode("reason").value;
                    if (option.length==0){
                        alert("没有做出选择");
                        return;
                    }
                    if (reason.length==0){
                        alert("请输入支付理由");
                        return;
                    }
                    setLoading(1);
                    const rtn = await voteVote("投票",vote.id.toNumber(),option[0],reason);
                    if (rtn){
                        goto("vote/detail",vote.id.toNumber());
                    }
                    setLoading(0);
                };
            }
            //返回按钮
            getNode("goback").onclick=function(){
                goto("vote/list");
            };
        }
    }
})();